/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Loans;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */

public class ViewDCF extends ActionSupport {

    private AreaDAO areadao = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private BorrowerDAO bordao = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private LoansDAO loandao = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private String acode;
    private String loc;
    private List<Loans> loanlist;
    private String viewdcf;
    private String datedcf;

    public String getDatedcf() {
        return datedcf;
    }

    public void setDatedcf(String datedcf) {
        this.datedcf = datedcf;
    }

    public String getViewdcf() {
        return viewdcf;
    }

    public void setViewdcf(String viewdcf) {
        this.viewdcf = viewdcf;
    }

    
    public String getAcode() {
        return acode;
    }

    public void setAcode(String acode) {
        this.acode = acode;
    }

    public String getLoc() {
        return loc;
    }

    public List<Loans> getLoanlist() {
        return loanlist;
    }

    public void setLoanlist(List<Loans> loanlist) {
        this.loanlist = loanlist;
    }

    @Action(value = "/view-dcf", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {

            Areas area = areadao.getByAreaCode(acode);
            HashMap<Integer, Borrowers> borperarea = bordao.getBorrowersPerArea(area.getAreaid());

            loanlist = new ArrayList<Loans>();

            for (Integer key : borperarea.keySet()) {
                loanlist.add(loandao.getLoansByBorrowerUnique(key));
            }

            ServletActionContext.getRequest().setAttribute("loanlist", loanlist);
            ServletActionContext.getRequest().setAttribute("area", area);
            ServletActionContext.getRequest().setAttribute("datedcf", datedcf);

            loc = "../../loancollection.jsp?cat=dcr";
            viewdcf ="yes";
            return SUCCESS;

        } catch (NullPointerException e) {
            viewdcf ="yes";
            loc = "../../loancollection.jsp?cat=dcr";

        } catch (Exception e) {
            viewdcf ="yes";
            loc = "../../loancollection.jsp?cat=dcr";

        }

        return SUCCESS;
    }
}
